const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled: true,
    code: '获取验证码',
    nick:"",
    student_id:"",
    phone:"",
    flag: 0

  },
  bindnick_input:function(e){
    console.log(e.detail.value.length);
    this.setData({
      nick:e.detail.value
    });
  },

  bindqq_input: function (e) {
    var content = e.detail.value;
    var flag = 0;
    this.setData({
      qq: e.detail.value,
      flag: flag
    });
  },

  bindphone_input: function (e) {
    var content = e.detail.value;
    var flag = 0;
    for (var i = 0; i < content.length; i++) {
      if (content[i] < '0' || content[i] > '9') {
        flag = 2;
      }
    }
    if(content.length == 11){
      this.setData({
        disabled: false
      })
    }
    this.setData({
      phone: e.detail.value,
      flag: flag
    });
  },

  bindcode_input: function(e){
    var that = this;
    var codes = e.detail.value;
    console.log(codes);
    that.setData({
      codes: codes
    })
  },


  Register:function(){
    var that = this;
    if(false){
      wx.showModal({
        title: '提示',
        content: '请输入正确的学号',
        showCancel: false,
      })
    }else if(that.data.phone.length != 11){
      wx.showModal({
        title: '提示',
        content: '请输入正确的电话号码',
        showCancel: false,
      })
    } else if (that.data.codes != that.data.check_code) {  //that.data.check_code
      wx.showModal({
        title: '提示',
        content: '请输入有效的验证码',
        showCancel: false,
      })
    }else{
      var openid = wx.getStorageSync('openid');//再从本地缓存 中取出
      console.log(openid);
      var userInfo = wx.getStorageSync("userInfo");
      var avatar_url = userInfo.avatarUrl;
      console.log(avatar_url);
      var is_Campus = wx.getStorageSync('is_Campus');
      wx.request({
        url: app.globalData.targetUrl + 'users/',
        method: 'POST',
        header: {
          'Content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          qq: that.data.qq,
          nick: that.data.nick,
          phone: that.data.phone,
          openid: openid,
          avatar_url: avatar_url,
          is_campus_type: is_Campus,
          password: "000000",
          campus_password: "00000"
        },
        success: function (res) {
          if(res.data.error == app.globalData.student_error){
            wx.showModal({
              title: '提示',
              content: '该学号已注册，请重新输入',
              showCancel: false,
            })
          }else{
            console.log(res.data);
            wx.setStorageSync("user_id", res.data.data.user_id);
            app.globalData.nowSkinUrl = "http://static.geek51.cn/wallpaper/01.jpeg";
            wx.showModal({
              title: '提示',
              content: '注册成功',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  wx.setStorageSync('is_Campus', is_Campus);
                  var is_Register = true;
                  wx.setStorageSync('is_Register', is_Register);
                  wx.switchTab({
                    url: "/pages/my/my"
                  });
                }
              }
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  onHide: function () {
  },

//事件选择校区，1为金石滩，0为开发区
  OnBindChange: function(e){
    var radio = e.detail.value;
    var is_Campus = -1;
    if(radio == "radio1"){
      is_Campus = 1;
    }else{
      is_Campus = 0;
    }
    console.log(is_Campus);
    wx.setStorageSync('is_Campus', is_Campus);
  },
  //事件选择校区，1为金石滩，0为开发区
 

  GetCode: function () {
    var that = this;
    var time = 60;
    that.setData({
      code: '60秒后重发',
      disabled: true
    })
    var phone = that.data.phone;

    wx.request({
      url: app.globalData.targetUrl + 'sms/check_code/',
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        phone: phone
      },
      success:function(res){
        console.log(res.data.data.check_code);  //
        that.setData({
          check_code: res.data.data.check_code
        })
      }
    })
    var Interval = setInterval(function () {
      time--;
      if (time > 0) {
        that.setData({
          code: time + '秒后重发'
        })
      } else {
        clearInterval(Interval);
        that.setData({
          code: '获取验证码',
          disabled: false
        })
      }
    }, 1000)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})